/**
 * localStorage 二次封装
 */

const namespace = "manager";

const getStorange = () => {
  let storage = window.localStorage.getItem(namespace) || "{}";
  return JSON.parse(storage);
};

const setItem = (key, val) => {
  const storage = getStorange();
  storage[key] = val;
  const newVal = JSON.stringify(storage);
  window.localStorage.setItem(namespace, newVal);
};

const getItem = (key) => {
  return getStorange()[key];
};

const clearItem = (key) => {
  let storage = getStorange();
  delete storage[key];
  const newVal = JSON.stringify(storage);
  window.localStorage.setItem(namespace, newVal);
};

const clearAll = () => {
  window.localStorage.clear();
};

export default {
  setItem,
  getItem,
  clearItem,
  clearAll,
};
